﻿using System;
using System.Globalization;
using Windows.Devices.Geolocation;
using BuddyTracker.Interfaces;
using BuddyTracker.Model;

namespace BuddyTracker.ViewModel.Encoders
{
    class HexLocationEncoder : ILocationEncoder
    {
        
            /// <summary>
            /// Encodes the location into application compatible string.
            /// </summary>
            /// <param name="location">Geopoint for encoding.</param>
            /// <returns>Application compatible string containgng location coordinates in plain text.</returns>
            public string EncodeLocation(Geopoint location)
            {
                return "FLWR|HEX|" + BitConverter.ToString(BitConverter.GetBytes(location.Position.Latitude)) + "|" + BitConverter.ToString(BitConverter.GetBytes(location.Position.Longitude)) + "|" + DateTime.Now.ToString("dd.MM.yyyy|HH:mm:ss", CultureInfo.InvariantCulture);
            }

            /// <summary>
            /// Decodes the application compatible string.
            /// </summary>
            /// <param name="s">String for decoding.</param>
            /// <returns>Geopoint with coordinates from the input string.</returns>
            public User DecodeString(string s)
            {
                try
                {
                    if (s.StartsWith("FLWR|HEX"))
                    {
                        var array = s.Split('|');
                        var latinbytes = array[2];
                        var loninbytes = array[3];
                 
                        latinbytes = latinbytes.Replace("-", "");
                        loninbytes = loninbytes.Replace("-", "");

                        var lat = BitConverter.ToDouble(StringToByteArray(latinbytes), 0);
                        var lon = BitConverter.ToDouble(StringToByteArray(loninbytes), 0);
                                                
                        var g = new Geopoint(new BasicGeoposition() { Latitude = lat, Longitude = lon });

                        //going to use this pattern for date mm.DD.yyyy and parse it manually!

                        var dateString = array[4];
                        var timeString = array[5];

                        var date = dateString.Split('.');
                        var day = int.Parse(date[0]);
                        var month = int.Parse(date[1]);
                        var year = int.Parse(date[2]);

                        var time = timeString.Split(':');
                        var hour = int.Parse(time[0]);
                        var minute = int.Parse(time[1]);
                        var second = int.Parse(time[2]);

                        var datetime = new DateTime(year, month, day, hour, minute, second);
                        var name = "placeholder";

                        return new User() { ScreenName = name, LastLocation = g, LastSeen = datetime };
                    }
                    else
                        return null;
                }
                catch
                {
                    return null;
                }
            }

            public static byte[] StringToByteArray(String hex)
            {
                int NumberChars = hex.Length;
                byte[] bytes = new byte[NumberChars / 2];
                for (int i = 0; i < NumberChars; i += 2)
                bytes[i / 2] = Convert.ToByte(hex.Substring(i, 2), 16);
                return bytes;
            }
         }
    }

